package org.zht.dormpro.dorm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zht.dormpro.auth.bean.PageBean;
import org.zht.dormpro.dorm.bean.ClassDTO;
import org.zht.dormpro.dorm.cacher.IClassCacher;
import org.zht.dormpro.dorm.mapper.TbClassMapper;
import org.zht.dormpro.dorm.model.TbClass;
import org.zht.dormpro.dorm.service.IClassService;

import java.util.ArrayList;
import java.util.List;

@Service
public class ClassServiceImpl implements IClassService {

    @Autowired
    public TbClassMapper tbClassMapper;

    @Autowired
    private IClassCacher classCacher;

    @Override
    public List<ClassDTO> getClassByCondition(PageBean pageBean) {
        Page<Object> objects = PageHelper.startPage(pageBean.getPageNum(), pageBean.getLimit());
        List<ClassDTO> classDTOS = tbClassMapper.getAllClassByCondition(pageBean);
        pageBean.setTotal((int) objects.getTotal());
        return classDTOS;
    }

    @Override
    public boolean addClass(TbClass tbClass) {
        int i = tbClassMapper.insertSelective(tbClass);
        return i > 0;
    }

    @Override
    public TbClass selectByPrimaryKey(Integer id) {
        return tbClassMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean editClass(TbClass tbClass) {
        int i = tbClassMapper.updateByPrimaryKeySelective(tbClass);
        return i > 0;
    }

    @Override
    public boolean deleteClass(Integer id) {
        int i = tbClassMapper.deleteByPrimaryKey(id);
        return i > 0;
    }

    @Override
    public boolean batchRemoveClass(ArrayList<String> claIds) {
        if (CollectionUtil.isNotEmpty(claIds)) {
            for (String claId : claIds) {
                boolean b = deleteClass(Integer.valueOf(claId));
                if (!b) {
                    return false;
                }
            }
        }
        return true;
    }

    //根据专业选择班级信息
    @Override
    public List<TbClass> getClassByMajId(String majId){
        //TODO 这里可以使用缓存
        List<TbClass> allClassByMajId = classCacher.getAllClassByMajId(majId);
        return allClassByMajId;

    }

    @Override
    public List<TbClass> getAllClass(int all) {
        return tbClassMapper.getAllClass(all);
    }
}
